﻿SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
GO
CREATE PROCEDURE [dbo].[bhp_PayU_PaymentTransactionGet] (
	@TransactionId int
) 
AS
BEGIN
	SELECT TOP 1
		*
	FROM
		bht_PayU_PaymentTransaction
	WHERE
		TransactionId = @TransactionId

END
GO
ALTER TABLE dbo.bht_PayU_PaymentTransaction ADD
	CreatedDate datetime NOT NULL CONSTRAINT DF_bht_PayU_PaymentTransaction_CreatedDate DEFAULT GETDATE(),
	BeginDate datetime NULL,
	SentDate datetime NULL,
	ReceiveDate datetime NULL,
	CancelDate datetime NULL
GO
ALTER TABLE dbo.bht_PayU_PaymentTransactionLog ADD
	CreatedDate datetime NOT NULL CONSTRAINT DF_bht_PayU_PaymentTransactionLog_CreatedDate DEFAULT GETDATE(),
	BeginDate datetime NULL,
	SentDate datetime NULL,
	ReceiveDate datetime NULL,
	CancelDate datetime NULL
GO

ALTER PROCEDURE [dbo].[bhp_PayU_PaymentTransactionLog] (
	@TransactionId int,
	@PosId int,
	@PosAuthKey nchar(7),
	@PaymentType nvarchar(10),
	@ChargeValue numeric(12, 2),
	@Description nvarchar(50),
	@FirstName nvarchar(100),
	@LastName nvarchar(100),
	@EMail nvarchar(100),
	@ClientIP nvarchar(15),
	@Status int,
	@OrderID int,
	@Description2 nvarchar(1024),
	@DescriptionBank nvarchar(27),
	@Street nvarchar(10),
	@StreetNumber nvarchar(10),
	@FlatNumber nvarchar(10),
	@City nvarchar(100),
	@PostCode nvarchar(20),
	@Country nvarchar(100),
	@Phone nvarchar(100),
	@Language char(10),
	@JavaScript bit,
	@PaybackLogin nvarchar(40),
	@Sig nvarchar(32),
	@Ts nvarchar(1000),
	@SessionId nvarchar(50),
	@CreatedDate datetime,
	@BeginDate datetime,
	@SentDate datetime,
	@ReceiveDate datetime,
	@CancelDate datetime
)
AS
BEGIN
	INSERT INTO bht_PayU_PaymentTransactionLog (
		[TransactionId],
		[PosId],
		[PosAuthKey],
		[PaymentType],
		[ChargeValue],
		[Description],
		[FirstName],
		[LastName],
		[EMail],
		[ClientIP],
		[Status],
		[OrderID],
		[Description2],
		[DescriptionBank],
		[Street],
		[StreetNumber],
		[FlatNumber],
		[City],
		[PostCode],
		[Country],
		[Phone],
		[Language],
		[JavaScript],
		[PaybackLogin],
		[Sig],
		[Ts],
		[SessionId],
		[CreatedDate],
		[BeginDate],
		[SentDate],
		[ReceiveDate],
		[CancelDate]
	) VALUES (
		@TransactionId,
		@PosId,
		@PosAuthKey,
		@PaymentType,
		@ChargeValue,
		@Description,
		@FirstName,
		@LastName,
		@EMail,
		@ClientIP,
		@Status,
		@OrderID,
		@Description2,
		@DescriptionBank,
		@Street,
		@StreetNumber,
		@FlatNumber,
		@City,
		@PostCode,
		@Country,
		@Phone,
		@Language,
		@JavaScript,
		@PaybackLogin,
		@Sig,
		@Ts,
		@SessionId,
		@CreatedDate,
		@BeginDate,
		@SentDate,
		@ReceiveDate,
		@CancelDate
	)
	
	SELECT @@IDENTITY as LogId
END
GO	
CREATE PROCEDURE [dbo].[bhp_PayU_PaymentTransactionSave] (
	@TransactionId int,
	@PosId int,
	@PosAuthKey nchar(7),
	@PaymentType nvarchar(10),
	@ChargeValue numeric(12, 2),
	@Description nvarchar(50),
	@FirstName nvarchar(100),
	@LastName nvarchar(100),
	@EMail nvarchar(100),
	@ClientIP nvarchar(15),
	@Status int,
	@OrderID int,
	@Description2 nvarchar(1024),
	@DescriptionBank nvarchar(27),
	@Street nvarchar(10),
	@StreetNumber nvarchar(10),
	@FlatNumber nvarchar(10),
	@City nvarchar(100),
	@PostCode nvarchar(20),
	@Country nvarchar(100),
	@Phone nvarchar(100),
	@Language char(10),
	@JavaScript bit,
	@PaybackLogin nvarchar(40),
	@Sig nvarchar(32),
	@Ts nvarchar(1000),
	@SessionId nvarchar(50),
	@CreatedDate datetime,
	@BeginDate datetime,
	@SentDate datetime,
	@ReceiveDate datetime,
	@CancelDate datetime
)
AS
BEGIN
	UPDATE 
		bht_PayU_PaymentTransaction 
	SET
		[PosId] = @PosId,
		[PosAuthKey] = @PosAuthKey,
		[PaymentType] = @PaymentType,
		[ChargeValue] = @ChargeValue,
		[Description] = @Description,
		[FirstName] = @FirstName,
		[LastName] = @LastName,
		[EMail] = @EMail,
		[ClientIP] = @ClientIP,
		[Status] = @Status,
		[OrderID] = @OrderID,
		[Description2] = @Description2,
		[DescriptionBank] = @DescriptionBank,
		[Street] = @Street,
		[StreetNumber] = @StreetNumber,
		[FlatNumber] = @FlatNumber,
		[City] = @City,
		[PostCode] = @PostCode,
		[Country] = @Country,
		[Phone] = @Phone,
		[Language] = @Language,
		[JavaScript] = @JavaScript,
		[PaybackLogin] = @PaybackLogin,
		[Sig] = @Sig,
		[Ts] = @Ts,
		[SessionId] = @SessionId
	WHERE
		TransactionId = @TransactionId
	
END
GO
CREATE TABLE dbo.bht_PayU_PaymentTransactionSnapshots
	(
	SnapshotId int NOT NULL IDENTITY (1, 1),
	[Transaction] int NOT NULL,
	Snapshot nvarchar(MAX) NOT NULL
	)  ON [PRIMARY]
	 TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE dbo.bht_PayU_PaymentTransactionSnapshots ADD CONSTRAINT
	PK_bht_PayU_PaymentTransactionSnapshots PRIMARY KEY CLUSTERED 
	(
	SnapshotId
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
ALTER TABLE dbo.bht_PayU_PaymentTransactionSnapshots ADD CONSTRAINT
	FK_bht_PayU_PaymentTransactionSnapshots_bht_PayU_PaymentTransaction FOREIGN KEY
	(
	[Transaction]
	) REFERENCES dbo.bht_PayU_PaymentTransaction
	(
	TransactionId
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO	
CREATE PROCEDURE [dbo].[bhp_PayU_PaymentTransactionSnapshotAdd] (
	@TransactionId	INT,
	@Snapshot	NVARCHAR(MAX)
)
AS
BEGIN
	INSERT INTO bht_PayU_PaymentTransactionSnapshots ([Transaction], [Snapshot]) VALUES (@TransactionId, @Snapshot)
END